package edu.infomatik.tundra.data;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class MockTundraData implements ITundraData<IMockedBean>{
	private List<IMockedBean> list = new ArrayList<IMockedBean>();

	@Override
	public void update(IMockedBean b) {
		long id = list.size()+1;
		b.setId(id);
		list.add(b);
	}

	@Override
	public List<IMockedBean> getAllBeans(IMockedBean b) {
		return list;
	}
	
	public List<Object> getAllMockedBeans(){
		List<Object> result = new ArrayList<Object>();
		for (Object bean : list) 
			result.add(((IMockedBean) bean).getMocked());
		return result;
	}

	@Override
	public IMockedBean getBeanById(IMockedBean mb) {
		for (IMockedBean b : list){
			if (b.getId() == mb.getId()) return b;
		}
		return null;
	}

	@Override
	public List<IMockedBean> getRange(IMockedBean mb, int indexFrom, int indexTo) {
		List<IMockedBean> result = new ArrayList<IMockedBean>();
		for (int i = indexFrom; i <= indexTo; i++){
			result.add(list.get(i));
		}
		return result;	
	}

	@Override
	public void delete(IMockedBean b) {
		for (IMockedBean mb : list){
			if (mb.getId() == b.getId()) list.remove(mb);
		}		
	}

}
